//
//  PVDDetailInverterNumberView.swift
//  ezyform
//
//  Created by MoennigQ on 8/4/20.
//  Copyright © 2020 Client.Solution Pty Ltd. All rights reserved.
//

import UIKit
import SnapKit
import RxCocoa
import RxSwift
class InvertersListView: PanleInverterListBaseView {
    fileprivate lazy var disposeBag = DisposeBag()
    var modelsDataObserver: BehaviorRelay<[InstallationDetailSystemDetailForInverterModel]> = BehaviorRelay(value: [])
    
    //spv top view
    let scannedCntLabel = BaseLabel()
    let inverterSerialLabel = ContentLabel(R.font.interMedium(size: 14),hexStr: "#292D3299")
    //table view
    var modelsTableView = TableView()
    var modelsTableViewHeightConstraint: Constraint?
    var invertersTableView = TableView()
    
    override func setUpView() {
        super.setUpView()
        modelsTableView.drawCorner(byRoundingCorners: [.topLeft,.topRight],radii: 16)
        modelsTableView.clipsToBounds = true
        modelsTableView.backgroundColor = UIColor(hexStr: Constant.TransparentColorHexStr)
        modelsTableView.register(JobDetailInverterTableViewCell.self, forCellReuseIdentifier: JobDetailInverterTableViewCell.reuseCellIdentifier)
        modelsTableView.isScrollEnabled = false
        modelsTableView.showsVerticalScrollIndicator = false
        self.contentBackView.addSubview(modelsTableView)
        modelsTableView.snp.makeConstraints { make in
            make.top.equalTo(20)
            make.left.equalTo(Constant.AllOutMargin)
            make.right.equalTo(-Constant.AllOutMargin)
            self.modelsTableViewHeightConstraint = make.height.equalTo(0).constraint
        }
        //serperate view
        let serperateView = View(hexStr: "#F5F5F5FF")
        self.contentBackView.addSubview(serperateView)
        serperateView.snp.makeConstraints { make in
            make.top.equalTo(modelsTableView.snp.bottom)
            make.left.right.equalToSuperview()
            make.height.equalTo(4)
        }
        
        //title label
        self.contentBackView.addSubview(inverterSerialLabel)
        inverterSerialLabel.snp.makeConstraints { (make) in
            make.left.equalTo(16)
            make.top.equalTo(serperateView.snp.bottom).offset(20)
            make.width.height.greaterThanOrEqualTo(0)
        }
        
        //
        self.contentBackView.addSubview(scannedCntLabel)
        scannedCntLabel.snp.makeConstraints { (make) in
            make.right.equalTo(-16)
            make.height.width.greaterThanOrEqualTo(0)
            make.centerY.equalTo(inverterSerialLabel)
        }
        //
        invertersTableView.showsVerticalScrollIndicator = false
        self.contentBackView.addSubview(invertersTableView)
        invertersTableView.snp.makeConstraints { make in
            make.top.equalTo(inverterSerialLabel.snp.bottom).offset(10)
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.height.greaterThanOrEqualTo(0)
            make.bottom.equalToSuperview()
        }
        //设置cell信息，绑定数据源
        modelsDataObserver.bind(to: modelsTableView.rx.items(cellIdentifier: JobDetailInverterTableViewCell.reuseCellIdentifier,cellType: JobDetailInverterTableViewCell.self)){(row, model, cell) in
            cell.setData(model,index: row)
        }.disposed(by: self.disposeBag)

        //观察数据变化，刷新数据
        modelsDataObserver.asObservable().subscribe(onNext: {[weak self] (list) in
            self?.modelsTableView.reloadData()
        }).disposed(by: disposeBag)
        //MARK: - 监听Contensize是获取Tableview 高度最准确的方法
        modelsTableView.rx.observe(CGSize.self,"contentSize").subscribe {[weak self] (size) in
            guard let contentSize = size else {return}
            self?.modelsTableViewHeightConstraint?.update(offset: contentSize.height)
        }.disposed(by: self.disposeBag)
        //注册cell
        invertersTableView.register(InstallationDetailTabInverterNumberCell.self, forCellReuseIdentifier: InstallationDetailTabInverterNumberCell.reuseCellIdentifier)
        //数据绑定
        JobDetailModelHelper.shared.pvd_inverter_serial_numbers.bind(to: invertersTableView.rx.items(cellIdentifier: InstallationDetailTabInverterNumberCell.reuseCellIdentifier,cellType: InstallationDetailTabInverterNumberCell.self)){(row, model, cell) in
            cell.setData(model)
        }.disposed(by: self.disposeBag)
        //绑定pvd_inverter_serial_numbers的数量显示
        JobDetailModelHelper.shared.pvd_inverter_serial_numbers.subscribe { panelList in
            let neededNumberCnt: Int = JobDetailModelHelper.shared.jobDetailModel.system_detail.inverter_plant_count
            let scannedNumberCnt = "\(JobDetailModelHelper.shared.jobDetailModel.pvd_inverter_serial_numbers.count)"
            let combineStr = scannedNumberCnt + "/\(neededNumberCnt)"
            
            let attrString = NSMutableAttributedString(string: combineStr )
            let attr1: [NSMutableAttributedString.Key: Any] = [.foregroundColor: UIColor(hexStr: "#379657")]
            let attr2: [NSMutableAttributedString.Key: Any] = [.foregroundColor: UIColor(hexStr: "#292D32")]
            
            attrString.addAttributes(attr1, range: NSRange(location: 0, length: scannedNumberCnt.count))
            attrString.addAttributes(attr2, range: NSRange(location: scannedNumberCnt.count, length: combineStr.count - scannedNumberCnt.count))
            self.scannedCntLabel.attributedText = attrString
            self.scannedCntLabel.setFont(R.font.interMedium(size: 13))
        }.disposed(by: self.disposeBag)
    }
    func updateUIByJobStatus(_ model: PVDDetailModel){
        if model.solar_water_heater {
            inverterSerialLabel.text = R.string.localizable.serialNumbers("Tank")
        }else{
            inverterSerialLabel.text = R.string.localizable.serialNumbers("Inverter")
        }
        //job finish或者当前用户不是installer
        if model.job_status == PVDJobStatusValue.installed.rawValue || !model.isInstallerFlag{
            self.hideBottomBtns(true)
        }else{
            self.hideBottomBtns(false)
        }
        
    }
}
